﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 9. Write a function for extracting all email addresses 
			from given text. All substrings that match the 
			format <identifier>@<host>…<domain> should 
			be recognized as emails. Return the emails as 
			array of strings. -->
	<style>
		textarea, button {
			display: block;
			width: 800px;
		}
		textarea{
			height: 100px;
		}
	</style>
</head>
<body>
	<textarea id="input" placeholder="Enter text with e-mails for extraction..."></textarea>
	<button onclick="doChange()">Replace Anchors</button>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// Checks it a word is valid e-mail
		function isValidEmail(word) {
			var rx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
			return rx.test(word);
		}

		// Extract all e-mails with RegEx
		function getEmails(text) {
			var rx = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
			var emails = text.match(rx);

			return emails;
		}

		// Button OnClick
		function doChange() {
			// Read User input
			var input = jsConsole.read("#input");

			// Output
			jsConsole.writeLine("You have entered:");
			jsConsole.writeLine("<blockquote>" + input + "</blockquote>");

			// Output - Find emails
			var result = getEmails(input);
			if (result) {
				jsConsole.writeLine("All emails:");
				jsConsole.writeLine("<blockquote>" + result.join(", ") + "</blockquote>");
				jsConsole.writeLine("E-mails Count = " + result.length);
			}
			else { jsConsole.writeLine("No emails."); }
		}
	</script>
</body>
</html>
